<?php
namespace App\Services;

use App\Models\User;
use App\Models\UserAskForGood;
use Illuminate\Support\Carbon;

class UserAskForGoodsService{
    public $userAskForGoodMode;
    public function __construct()
    {
        $this->userAskForGoodMode = new UserAskForGood();
    }

    /**
     * 用户产品询价
     * @param User $user  用户模型
     * @param int $goods_id 商品id
     * @return array
     */
    public function recordUserAskForGoods(User $user,int $goods_id){
        $has_this = UserAskForGood::where("user_id",$user->id)
            ->where("goods_id",$goods_id)
            ->where('created_at','>',Carbon::now()->subMinutes(10)->toDateTimeString())
            ->count();
        if (!$has_this){
            $this->userAskForGoodMode->users()->associate($user);
            $this->userAskForGoodMode->goods_id = $goods_id;
            $this->userAskForGoodMode->save();
            $state = [
                "msg"=>"记录成功",
            ];
        }else{
            $state = [
                "msg"=>"已记录",
            ];
        }
        return $state;
    }
}
